import { Injectable } from '@nestjs/common';
import {BookEntity} from './book.entity'
import {InjectRepository} from '@nestjs/typeorm'
import {Repository} from 'typeorm'

@Injectable()
export class BookService {
    constructor(
        @InjectRepository(BookEntity)
        private readonly bookRepository: Repository<BookEntity>
    ){}

    findList () {
        return this.bookRepository.find({relations: ['parts']})
    }

    findOne (id) {
        return this.bookRepository.findOne({id: id})
    }

    async create (): Promise<string> {
        let book = new BookEntity()
        book.bookName = '钢铁是怎样炼成的'
        book.summary = '小说通过记叙保尔·柯察金的成长道路告诉人们，一个人只有在革命的艰难困苦中战胜敌人也战胜自己，只有在把自己的追求和祖国、人民的利益联系在一起的时候，才会创造出奇迹，才会成长为钢铁战士。'
        book.readCount = 2000
        book.imgPath = 'https://img1.baidu.com/it/u=3324111745,4199526835&fm=26&fmt=auto&gp=0.jpg'
        return this.bookRepository.save(book).then(res => {
            return '新增成功'
        }).catch(err => {
            return '新增失败'
        })
    }
}
